@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion

REM 设置颜色
set "RED=[91m"
set "GREEN=[92m"
set "YELLOW=[93m"
set "BLUE=[94m"
set "NC=[0m"

REM 获取版本号
set "VERSION=%1"
if "%VERSION%"=="" set "VERSION=%date:~0,4%%date:~5,2%%date:~8,2%_%time:~0,2%%time:~3,2%%time:~6,2%"
set "VERSION=%VERSION: =0%"

echo %GREEN%=== LikeAdmin Python Windows 镜像构建和打包脚本 ===%NC%
echo %BLUE%版本号: %VERSION%%NC%

REM 检查Docker是否安装
docker --version >nul 2>&1
if errorlevel 1 (
    echo %RED%错误: Docker 未安装%NC%
    exit /b 1
)

REM 提示配置Docker镜像源
echo %YELLOW%提示: 为加快下载速度，建议配置Docker镜像源%NC%
echo %BLUE%配置文件位置: %programdata%\docker\config\daemon.json%NC%
echo %BLUE%参考配置: 复制 ../daemon.json 内容到 Docker Desktop 设置%NC%
echo.

REM 设置镜像标签
set "FASTAPI_IMAGE=likeadmin-python:fastapi-%VERSION%"
set "ADMIN_IMAGE=likeadmin-python:admin-%VERSION%"
set "NGINX_IMAGE=likeadmin-python:nginx-%VERSION%"

REM 构建FastAPI镜像
echo %YELLOW%构建 FastAPI 镜像...%NC%
cd ..\..\server
docker build -f ..\docker-prod\Dockerfile.fastapi -t %FASTAPI_IMAGE% .
if errorlevel 1 (
    echo %RED%FastAPI 镜像构建失败%NC%
    exit /b 1
)
echo %GREEN%FastAPI 镜像构建成功: %FASTAPI_IMAGE%%NC%

REM 构建Admin UI镜像
echo %YELLOW%构建 Admin UI 镜像...%NC%
cd ..\admin
docker build -f ..\docker-prod\Dockerfile.admin -t %ADMIN_IMAGE% .
if errorlevel 1 (
    echo %RED%Admin UI 镜像构建失败%NC%
    exit /b 1
)
echo %GREEN%Admin UI 镜像构建成功: %ADMIN_IMAGE%%NC%

REM 构建Nginx镜像
echo %YELLOW%构建 Nginx 镜像...%NC%
cd ..\docker-prod
docker build -f Dockerfile.nginx -t %NGINX_IMAGE% .
if errorlevel 1 (
    echo %RED%Nginx 镜像构建失败%NC%
    exit /b 1
)
echo %GREEN%Nginx 镜像构建成功: %NGINX_IMAGE%%NC%

REM 显示构建结果
echo %GREEN%=== 镜像构建完成 ===%NC%
echo %BLUE%FastAPI 镜像: %FASTAPI_IMAGE%%NC%
echo %BLUE%Admin UI 镜像: %ADMIN_IMAGE%%NC%
echo %BLUE%Nginx 镜像: %NGINX_IMAGE%%NC%

REM 保存镜像到文件
echo %YELLOW%保存镜像到文件...%NC%
docker save %FASTAPI_IMAGE% %ADMIN_IMAGE% %NGINX_IMAGE% -o likeadmin-python-%VERSION%.tar
if errorlevel 1 (
    echo %RED%镜像保存失败%NC%
    exit /b 1
)
echo %GREEN%镜像已保存到: likeadmin-python-%VERSION%.tar%NC%

REM 压缩镜像文件
echo %YELLOW%压缩镜像文件...%NC%
powershell -Command "Compress-Archive -Path 'likeadmin-python-%VERSION%.tar' -DestinationPath 'likeadmin-python-%VERSION%.zip' -Force"
if errorlevel 1 (
    echo %RED%镜像压缩失败%NC%
    exit /b 1
)
echo %GREEN%镜像已压缩到: likeadmin-python-%VERSION%.zip%NC%

REM 显示镜像大小
echo %YELLOW%镜像大小信息:%NC%
docker images | findstr likeadmin-python

echo %GREEN%=== 构建和打包完成 ===%NC%
echo %YELLOW%生成文件:%NC%
echo %BLUE%  - likeadmin-python-%VERSION%.tar (镜像文件)%NC%
echo %BLUE%  - likeadmin-python-%VERSION%.zip (压缩包)%NC%
echo.
echo %YELLOW%下一步操作:%NC%
echo %BLUE%  1. 使用 upload-only.bat 上传到服务器%NC%
echo %BLUE%  2. 使用 remote-deploy.bat 远程部署%NC%
echo %BLUE%  3. 手动上传文件到服务器%NC%

pause 